"use strict";

const Sequelize = require("sequelize");

module.exports = (app) => {
  const { INTEGER, STRING, DATE } = Sequelize;
  const NoticeModel = app.model.define(
    "notice",
    {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comments: "唯一标识符",
      },
      userId: {
        type: INTEGER,
        field: "user_id",
        allowNull: false,
        comments: "用户id",
        references: {
          model: app.model.UsersModel,
          key: "id",
          as: "users",
        },
      },
      title: {
        type: STRING,
        allowNull: false,
        comments: "标题",
      },
      content: {
        type: STRING,
        allowNull: false,
        comments: "内容",
      },
      isShow: {
        type: INTEGER,
        field: "is_show",
        allowNull: false,
        comments: "是否显示",
        defaultValue: 1,
      },
      createdAt: {
        field: "created_at",
        type: DATE,
      },
      updatedAt: {
        field: "updated_at",
        type: DATE,
      },
    },
    {
      tableName: "notice",
    }
  );

  NoticeModel.associate = function () {
    NoticeModel.belongsTo(app.model.UsersModel, {
      foreignKey: "user_id",
      as: "users",
      onDelete: "CASCADE",
    });
  };

  return NoticeModel;
};
